//
//  ViewController.m
//  Net2
//
//  Created by niit on 16/3/28.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import <TBXML.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)b1:(id)sender
{
    NSString *str=[NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",self.username.text,self.password.text];
    NSURL *url=[NSURL URLWithString:str];
    NSURLRequest *urlrequest=[NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:urlrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError==nil)
        {
            NSString *result=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if(dict[@"error"]!=nil)
            {
                NSString *message=[NSString stringWithFormat:@"错误信息:%@",dict[@"error"]];
                UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"!!!" message:message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
            }
            else
            {
                [self performSegueWithIdentifier:@"goto" sender:nil];
            }
        }
        else
        {
            NSLog(@"%@,%@",connectionError,[connectionError localizedDescription]);
        }
    }];
}
- (IBAction)b2:(id)sender
{
    NSString *str=@"http://localhost:8080/MJServer/login";
    NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@&type=XML",self.username.text,self.password.text];
    NSURL *url=[NSURL URLWithString:str];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod=@"POST";
    request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError==nil)
        {
            NSString *result=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            TBXML *xml=[[TBXML alloc]initWithXMLData:data error:nil];
            TBXMLElement *root=xml.rootXMLElement;
            NSString *info=[TBXML textForElement:root];
            NSString *elementname=[TBXML elementName:root];
            if([elementname isEqualToString:@"error"])
            {
                NSString *message=[NSString stringWithFormat:@"错误信息:%@",info];
                UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"!!!" message:message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
            }
            else
            {
                [self performSegueWithIdentifier:@"goto" sender:nil];
            }
        }
        else
        {
            NSLog(@"%@,%@",connectionError,[connectionError localizedDescription]);
        }
    }];
}
- (IBAction)b3:(id)sender
{
    NSString *str=@"http://localhost:8080/MJServer/login";
    NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@&type=JSON",self.username.text,self.password.text];
    NSURL *url=[NSURL URLWithString:str];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod=@"POST";
    request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError==nil)
        {
            NSString *result=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if(dict[@"error"]!=nil)
            {
                NSString *message=[NSString stringWithFormat:@"错误信息:%@",dict[@"error"]];
                UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"!!!" message:message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
            }
            else
            {
                [self performSegueWithIdentifier:@"goto" sender:nil];
            }
        }
        else
        {
            NSLog(@"%@,%@",connectionError,[connectionError localizedDescription]);
        }
    }];

}
@end
